package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.table.StaffLoginLogTableDef;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.dingreading.cloud.admin.entity.StaffLoginLog;
import com.dingreading.cloud.admin.mapper.StaffLoginLogMapper;
import com.dingreading.cloud.admin.service.StaffLoginLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-12-06
 */
@Service
public class StaffLoginLogServiceImpl extends BaseServiceImpl<StaffLoginLogMapper, StaffLoginLog, StaffLoginLogTableDef> implements StaffLoginLogService {

    @Override
    protected StaffLoginLogTableDef getTable() {
        return StaffLoginLogTableDef.staffLoginLog;
    }

    @Resource
    private StaffLoginLogMapper staffLoginLogMapper;

    @Override
    public Map<String, Date> lastListByStaffUid(List<String> usernameList) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.id))
                .from(table)
                .where(table.username.in(usernameList))
                .groupBy(table.username);
        List<StaffLoginLog> list = getMapper().selectListByCondition(table.username.in(usernameList).and(table.id.in(wrapper)));
//        List<StaffLoginLog> list = staffLoginLogMapper.lastListByStaffUid(usernameList);
        if (CollectionUtils.isNotEmpty(list))
        {
            Map<String, Date> map = list.stream().collect(Collectors.toMap(StaffLoginLog::getUsername, StaffLoginLog::getLoginTime));
            return map;
        }
        return null;
    }
}
